package oop.lab.domain.game;

import oop.lab.domain.board.Board;
import oop.lab.domain.board.Location;
import oop.lab.domain.board.PieceColor;

public class PeaceGame implements Game {
    Board board;
    private boolean finished = false;
    private PieceColor currentPieceColor;

    public String getGameType() {
        return "peace";
    }

    public PeaceGame() {
        this.board = new Board(8);
        currentPieceColor = PieceColor.BLACK;
    }

    public PeaceGame(Board board, PieceColor currentPieceColor) {
        this.board = board;
        this.currentPieceColor = currentPieceColor;
    }

    /* 用于测试的⽅法。 */

    public static PeaceGame ofGameWithFinishState() {
        PieceColor B = PieceColor.BLACK;
        PieceColor W = PieceColor.WHITE;
        PieceColor[][] grid = {
                { B, B, B, B, B, B, B, B },
                { B, B, B, B, B, B, B, B },
                { B, B, B, B, B, B, B, B },
                { B, B, B, W, null, B, B, B },
                { B, B, B, null, W, B, B, B },
                { B, B, B, B, B, B, B, B },
                { B, B, B, B, B, B, B, B },
                { B, B, B, B, B, B, B, B }
        };
        Board board = new Board(grid);
        return new PeaceGame(board, PieceColor.WHITE);
    }
    public boolean placePiece(Location loc) {
        if (finished) {
            throw new GameException("游戏已结束");
        }
        if (!board.isValidPlacement(loc)) {
            throw new GameException("无效位置:" + loc.toString());
        }
        board.placePiece(loc, currentPieceColor);
        currentPieceColor = currentPieceColor.oppositeColor();
        finished = isFinished();
        return true;
    }

    public Board getBoard() {
        return board;
    }

    private boolean isFinished() {
        return board.getAll(false).length == board.getSize() * board.getSize();
    }

    @Override
    public boolean isOver() {
        return finished;
    }

    public PieceColor getCurrentPlayer() {
        return currentPieceColor;
    }

    public PieceColor getWinner() {
        return null;
    }
}
